<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * User gateway class for communicating with persistent storage
 *
 * @category    RedCross
 * @package     Core_Gateway
 */
class Core_Gateway_User extends RedCross_Gateway_GatewayAbstract
{
    /**
     * Constructor
     * Defines some private variables
     */
    public function __construct()
    {
        $this->_class = 'User';
        $this->_modelPath = 'Core_Model';
    }

    public function fetchByEmail($email) {
        $email = (string) $email;
        $result = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->leftJoin('m.emails e')
            ->where('e.address = ?', $email)
            ->fetchOne();

        return $result;
    }

	public function fetchById($id) {
		$result = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->where($this->_primaryKey . ' = ?', $id)
			->fetchOne();
        return $result;
	}

    public function fetchUsersNotInGroup($groupId)
    {
        $result = Doctrine_Query::create()
            ->select('u.id, u.username, m.first_name, m.last_name')
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->where("u.id NOT IN (SELECT u2.id FROM {$this->getModelClass()} u2 INNER JOIN u2.Groups g WHERE g.id = ?)", $groupId)
            ->orderBy('m.last_name, m.first_name')
            ->fetchArray();
        return $result;
    }

    /*
     * @deprecated
     */
	public function fetchUsersAndMemberNames() {
		$result = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
			->fetchArray();
        return $result;
	}

    public function fetchUsersAll($paginate = false)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->leftJoin('u.Groups g')
            ->orderBy('u.username');
        
        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }

    public function fetchUsersOnlyConfirmed($paginate = false)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->leftJoin('u.Groups')
            ->where('u.is_confirmed = 1')
            ->orderBy('u.username');

        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }

    public function fetchUsersOnlyUnconfirmed($paginate = false)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->leftJoin('u.Groups g')
            ->where('u.is_confirmed = 0')
            ->orderBy('u.username');
        
        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }

    public function deleteUser($userId)
    {
    	$query = Doctrine_Query::create()
			->delete('Core_Model_User u')
			->addWhere('u.id = ?', $userId);
        
        return $query->execute();
	}

    public function fetchByIdIncludingMemberRelations($id) {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' u')
            ->leftJoin('u.Member m')
            ->leftJoin('m.emails e')
            ->leftJoin('m.Phonenumbers p')
            ->leftJoin('p.Phonetype pt')
            ->leftJoin('m.Postal po')
            ->where($this->_primaryKey . ' = ?', $id)
            ->orderBy('p.phonetype_id, e.is_main DESC');

        return $query->fetchOne();
	}
}
